package fun.sboy.headacheMemo.service.impl;

import fun.sboy.headacheMemo.entry.TaskTodo;
import fun.sboy.headacheMemo.entry.TaskTodoProgress;
import fun.sboy.headacheMemo.mapper.TaskTodoMapper;
import fun.sboy.headacheMemo.service.TaskTodoProgressService;
import fun.sboy.headacheMemo.service.TaskTodoService;
import fun.sboy.headacheMemo.util.RequestContextHolderUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;

@Service
public class TaskTodoServiceImpl implements TaskTodoService {

    @Autowired
    private TaskTodoMapper taskTodoMapper;
    @Autowired
    private TaskTodoProgressService taskTodoProgressService;

    /**
     * 完成
     */
    private static final Integer FINISHED = 1;
    /**
     * 未完成
     */
    private static final Integer UNFINISHED = 1;


    @Transactional
    @Override
    public void insertTaskTodo(TaskTodo taskTodo) {
        String currentUserId = RequestContextHolderUtil.getCurrentUserId();
        taskTodo.setUid(currentUserId);
        taskTodoMapper.insertTaskTodo(taskTodo);
        //同时插入一个记录进度
        taskTodoProgressService.insertProgress(taskTodo.getTlId());
    }

    /**
     * 更新任务
     *
     * @param taskTodo
     */
    @Transactional
    @Override
    public void updateTaskTodo(TaskTodo taskTodo) {
        String currentUserId = RequestContextHolderUtil.getCurrentUserId();
        taskTodo.setUid(currentUserId);

        //判断是否完成任务
        if (!ObjectUtils.isEmpty(taskTodo.getPercentage()) && taskTodo.getPercentage() == 100) {
            taskTodo.setState(FINISHED);
        }
        //更新当前任务
        taskTodoMapper.updateTaskTodo(taskTodo);

        //任务id
        String tlId = taskTodo.getTlId();

        //如果不存在进度信息，则表示没有更新进度
        if (ObjectUtils.isEmpty(taskTodo.getCurrentProgress())) {
            return;
        }
        //设置更新进度实例
        TaskTodoProgress taskTodoProgress = new TaskTodoProgress();
        taskTodoProgress.setPlId(tlId);

        taskTodoProgress.setProgressList(taskTodo.getCurrentProgress() + "||");
        taskTodoProgress.setPercentageList(taskTodo.getPercentage() + "||");

        //更新任务进度表
        taskTodoProgressService.updateProgressByTaskIdJoin(taskTodoProgress);
    }

    @Override
    public List<TaskTodo> selectTaskTodos() {
        String currentUserId = RequestContextHolderUtil.getCurrentUserId();
        return taskTodoMapper.selectTaskTodo(currentUserId);
    }

    @Override
    public List<TaskTodo> searchTaskTodos(String searchVal) {
        String currentUserId = RequestContextHolderUtil.getCurrentUserId();
        return taskTodoMapper.searchTaskTodo(searchVal, currentUserId);
    }

    @Transactional
    @Override
    public void deleteTaskTodo(String tId) {
        String currentUserId = RequestContextHolderUtil.getCurrentUserId();
        taskTodoMapper.deleteTaskTodoById(tId, currentUserId);
        taskTodoProgressService.deleteProgressByTaskId(tId);
    }

    @Override
    public TaskTodo selectTaskTodoById(Integer tlId) {
        String uId = RequestContextHolderUtil.getCurrentUserId();
        return taskTodoMapper.selectTaskTodoById(tlId, uId);
    }
}
